package beer.coder.imageviewer;

import beer.coder.imageviewer.handler.SceneDropHandler;
import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TabPane;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**
 * TODO
 *
 * @author amath0312
 * @version 1.0
 * @date 2020-11-01 12:07
 */
public class App extends Application {

  public static final double STAGE_WIDTH = 640d;
  public static final double STAGE_HEIGHT = 480d;

  protected Stage stage;
  protected Scene scene;
  protected Pane borderPane;
  protected TabPane imageViewPane;
  protected ImageViewer imageView;

  @Override
  public void start(Stage stage) {
    this.stage = stage;

    initStage(stage);

    String imagePath = getImagePath();
    String imageName = ImageViewerUtil.nameOfImage(imagePath);
    Image image = loadImage(imagePath);
    loadImageViewer(image, imageName);
  }

  public void loadImageViewer(Image image, String name) {
    imageView = new ImageViewer();
    borderPane.getChildren().clear();
    borderPane.getChildren().add(imageView);

    imageView.loadImage(
        image,
        imageView.getParent().getLayoutBounds().getWidth(),
        imageView.getParent().getLayoutBounds().getHeight()
    );
    imageView.initHandlers();
    stage.setTitle(ImageViewerUtil.titleOfImage(image, name));
  }

  protected String getImagePath() {
    String imagePath = getPropertyOrDefault("image", null);
    //imagePath = "E:\\Download\\image\\壁纸\\1029784.jpg";
    if (imagePath == null || "".equals(imagePath)) {
      imagePath = null;
    }
    return imagePath;
  }

  protected Image loadImage(String imagePath) {
    Image image;
    if (imagePath == null || "".equals(imagePath)) {
      image = ImageViewerUtil.makeEmptyImage(1, 1);
    } else {
      image = ImageViewerUtil.loadImage(new File(imagePath));
    }
    return image;
  }

  protected void initStage(Stage stage) {
    stage.initStyle(StageStyle.UNIFIED);
    stage.setResizable(true);
    stage.setAlwaysOnTop(false);
    stage.setWidth(getPropertyOrDefault("width", STAGE_WIDTH));
    stage.setHeight(getPropertyOrDefault("height", STAGE_HEIGHT));

    borderPane = new Pane();
    scene = new Scene(borderPane, getPropertyOrDefault("width", STAGE_WIDTH),
        getPropertyOrDefault("height", STAGE_HEIGHT));

    SceneDropHandler sceneDropHandler = new SceneDropHandler(this);
    scene.setOnDragOver(sceneDropHandler);
    scene.setOnDragDropped(sceneDropHandler);

    stage.setScene(scene);
    stage.getIcons()
        .add(new Image(Thread.currentThread().getContextClassLoader().getResource("icon.png").toExternalForm()));
    stage.show();
  }

  protected String getPropertyOrDefault(String name, String defval) {
    return this.getParameters().getNamed().getOrDefault(name, defval);
  }

  protected double getPropertyOrDefault(String name, double defval) {
    String val = this.getParameters().getNamed().get(name);
    if (val == null || "".equals(val.trim())) {
      return defval;
    } else {
      return Double.parseDouble(val);
    }
  }

  public Stage getStage() {
    return stage;
  }

  //public Pane getImageViewPane() {
  //  return imageViewPane;
  //}

  public ImageViewer getImageView() {
    return imageView;
  }
}
